﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MRSite.Common.Reflection
{
    public class AssemblyFinder : IAssemblyFinder
    {

        public List<Assembly> GetAssemblies()
        {
            var assemblies = new List<Assembly>();
            var path = string.IsNullOrWhiteSpace(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath) ?
                AppDomain.CurrentDomain.SetupInformation.ApplicationBase :
                AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
            List<string> excludeList = null;

            string excludeConfig = ConfigurationManager.AppSettings["excludeDll"];

            if (string.IsNullOrEmpty(excludeConfig) == false)
            {
                excludeList = excludeConfig.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
            }


            foreach (var file in Directory.GetFiles(path, "MRS*.dll"))
            {
                string fi = Path.GetFileName(file);
                //排除不需要扫描的dll
                if (excludeList != null && excludeList.Contains(fi))
                {
                    continue;
                }

                var assebmly = Assembly.LoadFrom(file);
                assemblies.Add(assebmly);
            }
            return assemblies;
        }


    }
}
